package com.example.camproject.network

import android.icu.text.StringSearch
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.*
import java.util.concurrent.TimeUnit
import retrofit2.http.FieldMap
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST
import okhttp3.MultipartBody







interface Apis{
    // username

    @POST("openway/")
    @FormUrlEncoded
    fun postAppBtn(@FieldMap userdata : Map<String , String>) : retrofit2.Call<AccessData>



    @Multipart
    @POST("createUser/")
    fun createUser(@Part partList : List<MultipartBody.Part>) : retrofit2.Call<User>


    //ok !!
    @POST("loginin/")
    @FormUrlEncoded
    fun login(@FieldMap maps : Map<String , String>) : retrofit2.Call<User>

    //username
    @POST("closeway/")
    @FormUrlEncoded
    fun closeWay(@FieldMap userdata : Map<String , String>) : retrofit2.Call<AccessData>

}



object NetClient{
    var client: OkHttpClient
    var retrofitCli: Retrofit
    val apis:Apis
    init {
        val inter= HttpLoggingInterceptor()
        inter.setLevel(HttpLoggingInterceptor.Level.BODY)
        client  = OkHttpClient.Builder()    //builder构造者设计模式
            .connectTimeout(10, TimeUnit.SECONDS) //连接超时时间
            .readTimeout(10, TimeUnit.SECONDS)    //读取超时
            .writeTimeout(10, TimeUnit.SECONDS)  //写超时，也就是请求超时
            .addInterceptor(inter)
            .build()
        retrofitCli= Retrofit.Builder()
            .client(client)
            .baseUrl("http://120.76.201.116:8888/apis/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        apis= retrofitCli.create(Apis::class.java)
    }
}



data class AccessData(
    val code: Int,
    val msg: String
)


data class User(
    val code: Int,
    val `data`: Data
)

data class Data(
    val access: Boolean,
    val imgurl: String,
    val username: String
)


